package com.meishij.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jash.myutils.utils.ViewUtils;
import com.meishij.R;
import com.meishij.customerview.FlowViewGroup;
import com.meishij.entry.FindShiCaiEntry;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 阿飞 on 2016/10/13.
 */

public class FindShiCaiRecyclerAdapter extends RecyclerView.Adapter<FindShiCaiRecyclerAdapter.ViewHolder> {

    private Context context;
    private List<FindShiCaiEntry.ShiCaiItemEntry> list;

    public FindShiCaiRecyclerAdapter(Context context, List<FindShiCaiEntry.ShiCaiItemEntry> list) {
        this.context = context;
        this.list = list;
        if (list == null) {
            this.list = new ArrayList<>();
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflateView = LayoutInflater.from(context).inflate(R.layout.activity_find_detail_shicai_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(inflateView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.initView(holder.rootView);
        FindShiCaiEntry.ShiCaiItemEntry item = list.get(position);
        String image = item.getImage();
        if (!TextUtils.isEmpty(image)) {
            Uri imageUri = Uri.parse(image);
            holder.iv_find_detail_item_image.setImageURI(imageUri);
        }
        ViewUtils.setTextView(holder.tv_find_detail_shicai_item_title, item.getTitle());
        ViewUtils.setTextView(holder.tv_find_detail_shicai_item_heatWord, item.getHeat_word());
        try {
            int heat_level = Integer.valueOf(item.getHeat_level());
            int imageId = 0;
            switch (heat_level) {
                case ViewHolder.HEATLEVEL_LOW:
                    imageId = R.mipmap.yyxx_icon_low;
                    break;
                case ViewHolder.HEATLEVEL_MIN:
                    imageId = R.mipmap.yyxx_icon_mid;
                    break;
                case ViewHolder.HEATLEVEL_HIGH:
                    imageId = R.mipmap.yyxx_icon_high;
                    break;
                default:
                    imageId = R.mipmap.yyxx_icon_low;
                    break;
            }
            Drawable drawable = context.getResources().getDrawable(imageId);
            holder.iv_find_detail_shicai_item_heatLevel.setImageDrawable(drawable);
        } catch (Exception e) {
            e.printStackTrace();
        }

        holder.layout_find_detail_shicai_item_tags.setVerticalSpacing(5);
        holder.layout_find_detail_shicai_item_tags.setHorizontalSpacing(5);
        List<String> gongxiaos = item.getGongxiao();
        for (String gongxiao : gongxiaos) {
            int subIndex = gongxiao.indexOf('\n');
            if (subIndex != -1) {
                gongxiao = gongxiao.substring(0, subIndex);
            }
            subIndex = gongxiao.indexOf(" ");
            if (subIndex != -1) {
                gongxiao = gongxiao.substring(0, subIndex);
            }
            subIndex = gongxiao.indexOf('\r');
            if (subIndex != -1) {
                gongxiao = gongxiao.substring(0, subIndex);
            }
            TextView gongxiaoItem = new TextView(context);
            gongxiaoItem.setBackgroundColor(Color.parseColor("#FFEFEFEF"));
            ViewUtils.setTextView(gongxiaoItem, gongxiao);
            gongxiaoItem.setPadding(5, 0, 5, 0);
            holder.layout_find_detail_shicai_item_tags.addView(gongxiaoItem);
        }

    }

    public void add(FindShiCaiEntry.ShiCaiItemEntry item) {
        list.add(item);
        notifyDataSetChanged();
    }

    public void addAll(List<FindShiCaiEntry.ShiCaiItemEntry> items) {
        list.addAll(items);
        notifyDataSetChanged();
    }

    public void clear() {
        list.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        View rootView;

        public static final int HEATLEVEL_LOW = 1;
        public static final int HEATLEVEL_MIN = 2;
        public static final int HEATLEVEL_HIGH = 3;

        SimpleDraweeView iv_find_detail_item_image;
        TextView tv_find_detail_shicai_item_title;
        FlowViewGroup layout_find_detail_shicai_item_tags;
        ImageView iv_find_detail_shicai_item_heatLevel;
        TextView tv_find_detail_shicai_item_heatWord;

        public ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
        }

        public void initView(View rootView) {
            iv_find_detail_item_image = (SimpleDraweeView) rootView.findViewById(R.id.iv_find_detail_item_image);
            tv_find_detail_shicai_item_title = (TextView) rootView.findViewById(R.id.tv_find_detail_shicai_item_title);
            layout_find_detail_shicai_item_tags = (FlowViewGroup) rootView.findViewById(R.id.layout_find_detail_shicai_item_tags);
            iv_find_detail_shicai_item_heatLevel = (ImageView) rootView.findViewById(R.id.iv_find_detail_shicai_item_heatLevel);
            tv_find_detail_shicai_item_heatWord = (TextView) rootView.findViewById(R.id.tv_find_detail_shicai_item_heatWord);
        }
    }

}
